<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        input {
            display: block;
            width: 400px;
            height: 30px;
            margin: 10px auto;
        }

        button {
            display: block;
            width: 400px;
            height: 30px;
            margin: 10px auto;
            background: blue;
            border-radius: 5px;
            border: none;
        }
    </style>
</head>

<body>
    <input type="text" placeholder="手机号/用户名/邮箱">
    <input type="text" placeholder="密码">
    <button>登录</button>
</body>

</html>
<script src="ajax.js"></script>
<script>
    // function Search(obj) {
    //     var str = ``
    //     for (const key in obj) {
    //         str += `${key}=${obj[key]}&`
    //     }
    //     return str.slice(0, str.length - 1)
    // }
    // function ajax(method, url, params, cb) {
    //     var xhr = new XMLHttpRequest()
    //     xhr.open(method,url + '?' + Search(params))
    //     xhr.onreadystatechange = function () {
    //         if (xhr.readyState == 4) {
    //             cb(JSON.parse(xhr.responseText))
    //         }
    //     }
    //     xhr.send()
    // }
    var inEle = document.querySelectorAll('input')
    var btn = document.querySelector('button')
    btn.onclick = function () {
        //如果不为空
        if (inEle[0].value && inEle[1].value) {
            console.log(inEle[0].value)
            console.log(inEle[1].value);
            if (/^\w+@[0-9a-z]+\.(com|cn|org)$/ || /^\d{4}$/ || /^1[3-9]\d{9}$/.test(inEle[0].value)  && /^\d{4}$/.test(inEle[1].value)) {
                ajax('get', 'https://yantianfeng.com/api/login', {
                    username: inEle[0].value, password: inEle[1].value
                }, function (data) {
                    if (data.code == 200) {
                        console.log('登录成功');
                    } else {
                        console.log('登录失败');
                    }
                })
            } else {
                alert('请输入正确手机号/密码')
            }
        } else {
            alert('用户名/密码不能为空!')
        }
    }

</script>